package lena;

import lib.Actor;
import lib.ActorAddress;
import lib.Message;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
 
public class OpenerActor extends Actor{

	public OpenerActor(ActorAddress superActor,ActorAddress myAddress) throws Exception {
		super(superActor,myAddress,"OpenerActor");
		startActor();
	}

	protected boolean standardRoutine() throws Exception{
		this.standardRoutineIsUsed=false;
		standardRoutineIsUsed= false;
		return true;
	}

	protected boolean handleMessage(Message msg) throws Exception {
		if (msg.getOperation().equals("Terminate")) {
			System.out.println("OpenerActor will halt");
			return false;
		}
		
		try {
			System.out.println("OpenerActor: opening "+(String)msg.getContent());
			String filename = (String)msg.getContent();
			BufferedImage img = ImageIO.read(new File(filename));
			SentImage serializableImage = new SentImage(img);
			System.out.println("OpenerActor: Sending image to DisplayerActor");
			sendMsg(new Message(myAddress, "OpenerActor", "CutterActor", msg.getOperation(), serializableImage));
			
		} catch (IOException e) {
			System.out.println("OpenerActor: File "+(String)msg.getContent()+" not found");
			return true;
		}
		
		return true;
	}
}

